Wrong strings test in zsh script
[33]
I was porting one of my bash scripts to my brand-new zsh
and I got stuck with the following lines:
CMDLINE=`cat /proc/$AGPID/cmdline 2> /dev/null`
if [ $? == 1 ] || [ "$CMDLINE" != "ssh-agent" ]; then
...
|
|
so, what's the difficulty with this code? I literally translated
that code with:
CMDLINE=`< /proc/$AGPID/cmdline`
[[ $? = 1 || "$CMDLINE" != "ssh-agent" ]] && ...
|
|
``< file'' is equal to ``cat < file'' and ``cat file''.
this test fails:
"$CMDLINE" != "ssh-agent"
|
|
well, what's wrong is the ``$CMDLINE'' variable, which holds the file
as red, ``ssh-agent\0'' instead of ``ssh-agent''.
It seems bash strips out that character, but zsh doesn't.
In my case the workaround is easy, use strings instead of cat and
don't get bothered anymore.
CMDLINE=`strings /proc/$AGPID/cmdline`
|
|
This note is available in the following categories: